package com.business.system.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.business.base.action.BaseAction;
import com.business.base.tools.JsonUtil;
import com.business.system.pojo.TabRepairClyy;
import com.business.system.service.ITabRapairCLYYService;
import com.utility.Page;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
public class RepairCLYYAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private TabRepairClyy repairCLYY;
	@Autowired
	private ITabRapairCLYYService repairCLYYService;
	private String id;
	private String yyNo;

	@Action(value = "RepairCLYYAction_list")
	public String list() throws Exception {
		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (repairCLYY != null && !paramsCode.equals("")) {
			repairCLYY = (TabRepairClyy) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairClyy.class);
		} else {
			repairCLYY = new TabRepairClyy();
		}

		Page<TabRepairClyy> page = this.repairCLYYService.findListPage(
				this.repairCLYY, this.pageNo, this.limit);
		List<TabRepairClyy> list = page.getResults();
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + page.getTotalRecord() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairCLYYAction_getByNo")
	public String getByNo() throws Exception {
		repairCLYY = new TabRepairClyy();
		repairCLYY.setYyNo(this.yyNo);

		List<TabRepairClyy> list = this.repairCLYYService.findList(repairCLYY);
		if (list.size() == 0) {
			String result = "{success:false}";
			this.jsonOut(result);
			return NONE;
		} else {
			JSONArray array = JSONArray.fromObject(list.get(0));
			String result = "{success:true,root:" + array.toString() + "}";
			this.jsonOut(result);
			return NONE;
		}
	}

	@Action(value = "RepairCLYYAction_add")
	public String add() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			repairCLYY = (TabRepairClyy) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairClyy.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.repairCLYYService.insert(repairCLYY);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairCLYYAction_edit")
	public String edit() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			repairCLYY = (TabRepairClyy) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairClyy.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.repairCLYYService.update(this.repairCLYY);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairCLYYAction_delete")
	public String delete() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.repairCLYYService.delete(id);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public TabRepairClyy getRepairCLYY() {
		return repairCLYY;
	}

	public void setRepairCLYY(TabRepairClyy repairCLYY) {
		this.repairCLYY = repairCLYY;
	}

	public String getYyNo() {
		return yyNo;
	}

	public void setYyNo(String yyNo) {
		this.yyNo = yyNo;
	}

}
